package com.basic.common.websocket;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

import javax.annotation.Resource;

/**
 * websocket 配置
 *
 * @author zhaoxy_jn@163.com
 */
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer
{
    @Resource
    private HttpAuthHandler httpAuthHandler;
    @Resource
    private WsInterceptor wsInterceptor;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry
                // SecurityConfig添加 antMatchers("/websocket/**").permitAll() 不验证，在这里进行验证
                // 在拦截器wsInterceptor中验证token
                .addHandler(httpAuthHandler, "/websocket/**")  // websocket 请求处理器和请求url
                .addInterceptors(wsInterceptor)     // 拦截器(请求合法性校验)
                .setAllowedOrigins("*");    // 关闭跨域校验
    }
}
